package com.nbot.server.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nbot.server.domain.KeyParameterDO;
import com.nbot.server.service.KeyParameterService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;

/**
 * 重要参数
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-06-17 14:10:53
 */

@Controller
@RequestMapping("/server/keyParameter")
public class KeyParameterController {
    @Autowired
    private KeyParameterService keyParameterService;

    @GetMapping()
    @RequiresPermissions("server:keyParameter:keyParameter")
    String KeyParameter() {
        return "server/keyParameter/keyParameter";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("server:keyParameter:keyParameter")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<KeyParameterDO> keyParameterList = keyParameterService.list(query);
        int total = keyParameterService.count(query);
        PageUtils pageUtils = new PageUtils(keyParameterList, total);
        return pageUtils;
    }


    @GetMapping("/chart/{autoId}")
    @RequiresPermissions("server:keyParameter:keyParameter")
    String catChart(@PathVariable("autoId") Long autoId, Model model) {
        model.addAttribute("autoId", autoId);
        return "server/keyParameter/chart";
    }


    @GetMapping("/add")
    @RequiresPermissions("server:keyParameter:add")
    String add() {
        return "server/keyParameter/add";
    }

    @GetMapping("/edit/{autoId}")
    @RequiresPermissions("server:keyParameter:edit")
    String edit(@PathVariable("autoId") Long autoId, Model model) {
        KeyParameterDO keyParameter = keyParameterService.get(autoId);
        model.addAttribute("keyParameter", keyParameter);
        return "server/keyParameter/edit";
    }



    @GetMapping("/catLog/{autoId}")
    @RequiresPermissions("server:keyParameter:catLog")
    String catLog(@PathVariable("autoId") Long autoId, Model model) {
        KeyParameterDO keyParameter = keyParameterService.get(autoId);
        model.addAttribute("keyParameter", keyParameter);
        return "server/keyParameterLog/";
    }
    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:keyParameter:add")
    public R save(KeyParameterDO keyParameter) {
        if (keyParameterService.save(keyParameter) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:keyParameter:edit")
    public R update(KeyParameterDO keyParameter) {
        keyParameterService.update(keyParameter);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:keyParameter:remove")
    public R remove(Long autoId) {
        if (keyParameterService.remove(autoId) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:keyParameter:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] autoIds) {
        keyParameterService.batchRemove(autoIds);
        return R.ok();
    }

}
